package com.atguigu.gmall.controler;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.entiity.DAUPerHour;
import com.atguigu.gmall.entiity.GMVPerHour;
import com.atguigu.gmall.server.PublisherService;
import com.atguigu.gmall.server.PublisherServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @ClassName: PublicController
 * @Description:
 * @Author: kele
 * @Date: 2021/3/19 20:44
 **/

@RestController
public class PublicController {

    //自动创建对象，通过接口创建对象，
    @Autowired
    private PublisherService pub;


    //处理http://localhost:8070/realtime-total?date=2020-08-15的请求
    @RequestMapping(value = "/realtime-total")
    public ArrayList handle1(String date){

        Integer daUbyDate = pub.getDAUbyDate(date);
        Integer midCountbyDate = pub.getMidCountbyDate(date);
        Double gmVbyDate = pub.getGMVbyDate(date);

        /**
         * 结果为list类型，
         *        [{"id":"dau","name":"新增日活","value":1200},
         *         {"id":"new_mid","name":"新增设备","value":233}
         *         {"id":"order_amount","name":"新增交易额","value":1000.2 }
         *        ]
         */

        ArrayList arrayList = new ArrayList();

        //日活用户
        HashMap<String, Object> map1 = new HashMap<>();
        //新增设备
        HashMap<String, Object> map2 = new HashMap<>();
        //交易额
        HashMap<String, Object> map3 = new HashMap<>();

        map1.put("id","dau");
        map1.put("name","日活");
        map1.put("value",daUbyDate);

        map2.put("id","new_mid");
        map2.put("name","新增设备");
        map2.put("value",midCountbyDate);

        map3.put("id","order_amount");
        map3.put("name","新增交易额");
        map3.put("value",gmVbyDate);

        arrayList.add(map1);
        arrayList.add(map2);
        arrayList.add(map3);

        return arrayList;

    }



//处理 http://localhost:8070/realtime-hours?id=dau&date=2020-08-15
    @RequestMapping(value = "/realtime-hours")
    public JSONObject handler2(String id,String date){


        //处理id为order_amount的请求
        if(id.equals("order_amount")){
            return gmvHandler(date);
        }

        //处理id为dau的请求
        if(id.equals("dau")){

            return dauHandler(date);

        }

        return null;

    }



    public JSONObject dauHandler(String date){

        //获取昨天的日期
        String yesday = LocalDate.parse(date).minusDays(1).toString();

        //查询今天的数据
        List<DAUPerHour> todayCount = pub.getMidCountByHour(date);
        //查询昨天的数据
        List<DAUPerHour> yesdayCount = pub.getMidCountByHour(yesday);

        JSONObject jsonYesday = dauparseJSON(yesdayCount);
        JSONObject jsonToday = dauparseJSON(todayCount);

        JSONObject result = new JSONObject();

        result.put("yesterday",jsonYesday);
        result.put("today",jsonToday);

        return result;
    }

    /**
     * 处理id为order_amount的请求，属于GMV
     * @param date
     * @return
     */
    public JSONObject gmvHandler(String date){

        /**
         * 数据格式为：
         *     {"yesterday":{"11":383,"12":123,"17":88,"19":200 },
         *      "today":{"12":38,"13":1233,"17":123,"19":688 }}
         */

        //获取昨天的日期
        String yesday = LocalDate.parse(date).minusDays(1).toString();

        //查询昨天的数据
        List<GMVPerHour> yesgmvSumByHour = pub.getGMVSumByHour(yesday);
        //查询今天的数据
        List<GMVPerHour> todaygmvSumByHour = pub.getGMVSumByHour(date);

        JSONObject yesJson = gmvParseJSON(yesgmvSumByHour);
        JSONObject todayJson = gmvParseJSON(todaygmvSumByHour);

        JSONObject result = new JSONObject();

        result.put("yesterday",yesJson);
        result.put("today",todayJson);

        return result;
    }



    /**
     *
     * 将结果转化为json格式，
     *
     * json与list格式对比：
     *    list输出为：
     *      [DAUPerHour(hour=zhangsan, count=12), DAUPerHour(hour=zhangsan2, count=13), DAUPerHour(hour=zhangsan3, count=14)]
     *    json输出为：
     *      {"zhangsan3":14,"zhangsan2":13,"zhangsan":12}
     *
     * @param datas
     * @return today和yestaday的json格式
     **/

    public JSONObject dauparseJSON(List<DAUPerHour> datas){

        JSONObject jsonObject = new JSONObject();

        for (DAUPerHour data : datas) {

            jsonObject.put(data.getHour(),data.getDaucount());

        }

        return jsonObject;

    }

    public JSONObject gmvParseJSON(List<GMVPerHour> datas){

        JSONObject jsonObject = new JSONObject();

        for (GMVPerHour data : datas) {

            jsonObject.put(data.getHour(),data.getGmvSum());

        }

        return jsonObject;

    }

    @RequestMapping(value = "/sale_detail")
    public JSONObject handler3(String date, Integer startpage, Integer size, String keyword) throws IOException {

        JSONObject analysis = pub.getAnalysis(date, startpage, size, keyword);

        return analysis;


    }


}
